package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz.Question;
import com.example.satu.quizcreator.quiz_creator.interfaces.onAddEditQuestionButtonClick;
import com.example.satu.quizcreator.quiz_creator.interfaces.onAnswerMethodButtonClick;
import com.example.satu.quizcreator.quiz_creator.interfaces.onBackPressedListener;
import com.example.satu.quizcreator.quiz_creator.interfaces.onQuestionListItemClickListener;
import com.github.rtyley.android.sherlock.roboguice.fragment.RoboSherlockFragment;

import java.util.ArrayList;

import roboguice.inject.InjectView;

import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE;

/**
 * Created by Satu on 2014-09-03.
 */

//@ContentView(R.layout.question_creator_layout)
public class QuestionCreatorFragment extends RoboSherlockFragment implements
        onQuestionListItemClickListener, onAnswerMethodButtonClick,
        onAddEditQuestionButtonClick, onBackPressedListener {
    private QuestionListFragment listFragment;

    public static final String TRANSAKCJA_1 = "transakcja1";

    @InjectView(R.id.fragment_container2)
    private View frameLayoutWithDetails;

    Answer_method_fragment answer_method_fragment;

    //onNewQuestionCreate newQuestionCreate;

    CreateQuestionFragment createQuestionFragment;

    EditQuestionFragment editQuestionFragment;

    Multi_answer_fragment multi_answer_fragment;
    Basket_answer_fragment basket_answer_fragment;

    boolean answerMethodChosen = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_question_creator_layout,null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listFragment = new QuestionListFragment();
        switchMainFragment(listFragment,R.id.fragment_container);

    }

    private void switchMainFragment(Fragment fragment, int resID){
        switchMainFragment(fragment,null,resID);
    }

    private void switchMainFragment(Fragment fragment,String transaction_name,int resID){
        getChildFragmentManager()
                .beginTransaction()
                .replace(resID, fragment)
                .addToBackStack(transaction_name)
                .commit();
    }

    public void onListItemClick(Question question) {

        if(question == null) {//Create question
            createQuestionFragment = new CreateQuestionFragment();
            switchMainFragment(createQuestionFragment, R.id.fragment_container2);
            if (frameLayoutWithDetails.getVisibility() != View.VISIBLE) {
                frameLayoutWithDetails.setVisibility(View.VISIBLE);
            }
            answer_method_fragment = new Answer_method_fragment();
            switchMainFragment(answer_method_fragment, R.id.fragment_container_answer_method);
        }else {     // edit question
            editQuestionFragment = new EditQuestionFragment(question);
            switchMainFragment(editQuestionFragment, R.id.fragment_container2);
            if (frameLayoutWithDetails.getVisibility() != View.VISIBLE) {
                frameLayoutWithDetails.setVisibility(View.VISIBLE);
            }
            //Fragment fragment;
            if(question.getAnswerMethodFlag() == Question.MULTI_ANSWER_METHOD){
                multi_answer_fragment = new Multi_answer_fragment(question.getAnswers());
                switchMainFragment(multi_answer_fragment, R.id.fragment_container_answer_method);
            }
            else{
                basket_answer_fragment = new Basket_answer_fragment(question.getBadAnswers(),question.getGoodAnswers());
                switchMainFragment(basket_answer_fragment, R.id.fragment_container_answer_method);
            }

        }
    }

    @Override
    public void multiAnswerButtonClick(int flag) {
 //       createQuestionFragment.setANSWER_METHOD_FLAG(flag);
        answerMethodChosen = true;
        if(flag == Question.MULTI_ANSWER_METHOD){
            multi_answer_fragment = new Multi_answer_fragment();
            switchMainFragment(multi_answer_fragment,TRANSAKCJA_1,R.id.fragment_container_answer_method);
        }else{
            basket_answer_fragment = new Basket_answer_fragment();
            switchMainFragment(basket_answer_fragment,TRANSAKCJA_1,R.id.fragment_container_answer_method);
        }
    }
    @Override
    public void onAddEditQuestionButton(boolean edit) {

        Question question;
        if (edit) {
            Question item = listFragment.getQuestionAdapter().getItem((int) editQuestionFragment.getQuestionIndex());
            item.setTitle(editQuestionFragment.getQuestionBody());
            if (answer_method_fragment.getANSWER_METHOD_FLAG() == Question.MULTI_ANSWER_METHOD){
                item.setAnswers(multi_answer_fragment.getAnswers());
            }else {
                item.setGoodAnswers(basket_answer_fragment.getGoodAnswers());
                item.setBadAnswers(basket_answer_fragment.getBadAnswers());
            }
  //          Toast.makeText(getActivity(), "edytowano pytanie z listy", Toast.LENGTH_SHORT).show();
            //question = getQuestion(editQuestionFragment.getQuestionBody(),editQuestionFragment.getQuestionIndex());
            //listFragment.getQuestionAdapter().remove(listFragment.getQuestionAdapter().getItem(((int) editQuestionFragment.getQuestionIndex())));
        } else {
            question = getQuestion(createQuestionFragment.getQuestionBody(), 0);
            listFragment.getQuestionAdapter().add(question);
//            Toast.makeText(getActivity(), "dodanie pytania do listy ", Toast.LENGTH_SHORT).show();
        }

    }
    private Question getQuestion(Bundle questionBundle, long questionIndex ){
        Question question = new Question();
        question.setTitle(questionBundle.getString(CreateQuestionFragment.QUESTION_BODY));
        question.setAnswerMethodFlag(answer_method_fragment.getANSWER_METHOD_FLAG());
        FILE_TYPE file_type = FILE_TYPE.values()[questionBundle.getInt(CreateQuestionFragment.FILE_TYPE)];
        question.setFile_type(file_type);
        question.setFilePath(questionBundle.getString(CreateQuestionFragment.FILE_PATH));
/*        switch (file_type){ // refactor
            case NON_FILE: break;
            case IMAGE_TYPE:
                question.setFile_type(FILE_TYPE.IMAGE_TYPE);
                question.setPicture((android.graphics.Bitmap) questionBundle.getParcelable(CreateQuestionFragment.IMAGE));
                break;
            case AUDIO_TYPE:
                question.setFile_type(FILE_TYPE.AUDIO_TYPE);
                question.setVideo_sound(questionBundle.getByteArray(CreateQuestionFragment.AUDIO_VIDEO));
                break;
            case VIDEO_TYPE:
                question.setFile_type(FILE_TYPE.VIDEO_TYPE);
                question.setVideo_sound(questionBundle.getByteArray(CreateQuestionFragment.AUDIO_VIDEO));
                break;
        }*/
        if(answer_method_fragment.getANSWER_METHOD_FLAG() == Question.MULTI_ANSWER_METHOD){
            question.setAnswers(multi_answer_fragment.getAnswers());
        }else{
            question.setGoodAnswers(basket_answer_fragment.getGoodAnswers());
            question.setBadAnswers(basket_answer_fragment.getBadAnswers());
        }
        if (questionIndex != 0)
            question.setId(questionIndex);
        else
            question.setId(listFragment.getQuestionAdapter().getCount());
        return question;
    }
    public ArrayList<Question> getQuestions(){
       return listFragment.getQuestionAdapter().getQuestions();
    }

    @Override
    public boolean onBack() {
        if(answerMethodChosen) {
            getChildFragmentManager().popBackStack(TRANSAKCJA_1, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            answerMethodChosen = false;
            return true;
        }
        return false;
    }
}
